package com.xiong.wuliao.mainfragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xiong.wuliao.HomeGame.StartActivity;
import com.xiong.wuliao.HomeIntelligent.IntelligentActivity;
import com.xiong.wuliao.HomeVideo.WebPlayerActivity;
import com.xiong.wuliao.R;
import com.xiong.wuliao.homeweather.WeatherActivity;

import java.util.ArrayList;

/**
 * 第三个页面：综合页面，包含交互天气预报，智能机器人对话，小游戏等
 */
public class GoodFragment extends Fragment implements View.OnClickListener {
    private ImageView homeWeather;
    private ImageView homeIntelligent;
    private ImageView homeGame;

    private ListView myListViewForVideo;
    private static ArrayList<String[]> mOnlineList = new ArrayList<>();
    private static ArrayList<Integer> mOnlineLogoList = new ArrayList<>();

    private View headView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_good, container, false);
        findView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addHead();
        setAdapter();
        setListener();
    }

    private void addHead() {
        headView = View.inflate(getActivity(),R.layout.good_fragment_head,null);
        homeWeather = (ImageView) headView.findViewById(R.id.homeWeather);
        homeIntelligent = (ImageView) headView.findViewById(R.id.homeIntelligent);
        homeGame = (ImageView) headView.findViewById(R.id.homeGame);
        homeWeather.setOnClickListener(this);
        homeIntelligent.setOnClickListener(this);
        homeGame.setOnClickListener(this);
        myListViewForVideo.addHeaderView(headView);
    }

    private void setListener() {
        myListViewForVideo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final String[] f = mOnlineList.get(position-1);
                String videoUrl = f[1];
                Intent intent = new Intent(getActivity(), WebPlayerActivity.class);
                intent.putExtra("videoUrl",videoUrl);
                startActivity(intent);
            }
        });
    }

    private void setAdapter() {
        myListViewForVideo.setAdapter(new DataAdapter(getActivity(),mOnlineList));
    }

    /**
     * 初始化控件
     */
    private void findView(View view) {
        myListViewForVideo = (ListView) view.findViewById(R.id.myListViewForVideo);
    }

    /**
     * 跳转到天气，智能机器人，游戏
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.homeWeather:
                intent.setClass(getActivity(), WeatherActivity.class);
                startActivity(intent);
                break;
            case R.id.homeIntelligent:
                intent.setClass(getActivity(), IntelligentActivity.class);
                startActivity(intent);
                break;
            case R.id.homeGame:
                intent.setClass(getActivity(), StartActivity.class);
                startActivity(intent);
                break;
        }
    }

    private class DataAdapter extends BaseAdapter {
        public Context context;
        public LayoutInflater inflater;
        public ArrayList<String[]> mOnlineList;

        public DataAdapter(Context context,ArrayList<String[]> mOnlineList) {
            this.context = context;
            this.mOnlineList = mOnlineList;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return mOnlineList.size();
        }

        @Override
        public Object getItem(int position) {
            return mOnlineList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null){
                convertView = inflater.inflate(R.layout.fragment_online_item,null);
                holder = new ViewHolder();
                holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
                holder.arrow = (ImageView) convertView.findViewById(R.id.arrow);
                holder.title1 = (TextView) convertView.findViewById(R.id.title1);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.thumbnail.setImageResource(mOnlineLogoList.get(position));
            holder.title1.setText(mOnlineList.get(position)[0]);
            return convertView;
        }
    }
    static class ViewHolder{
        public ImageView thumbnail;
        public ImageView arrow;
        public TextView title1;
    }

    static {
        // 120 60
        mOnlineList.add(new String[] { "优酷视频", "http://www.youku.com" });//http://3g.youku.com
        mOnlineLogoList.add(R.drawable.logo_youku);
        // 104 43
        mOnlineList.add(new String[] { "搜狐视频", "http://m.tv.sohu.com" }); //http://m.tv.sohu.com
        mOnlineLogoList.add(R.drawable.logo_sohu);
        mOnlineList.add(new String[] { "乐视TV", "http://m.letv.com" });
        mOnlineLogoList.add(R.drawable.logo_letv);
        // 174 48
        mOnlineList.add(new String[] { "爱奇异", "http://3g.iqiyi.com" });
        mOnlineLogoList.add(R.drawable.logo_iqiyi);
        mOnlineList.add(new String[] { "PPTV", "http://m.pptv.com/" });
        mOnlineLogoList.add(R.drawable.logo_pptv);
        // 181 60
        mOnlineList.add(new String[] { "腾讯视频", "http://3g.v.qq.com/" });
        mOnlineLogoList.add(R.drawable.logo_qq);
        mOnlineList.add(new String[] { "56.com", "http://m.56.com/" });
        mOnlineLogoList.add(R.drawable.logo_56);
        mOnlineList.add(new String[] { "新浪视频", "http://video.sina.cn/" });
        mOnlineLogoList.add(R.drawable.logo_sina);
        mOnlineList.add(new String[] { "土豆视频", "http://m.tudou.com" });
        mOnlineLogoList.add(R.drawable.logo_tudou);
    }
}
